package battleship;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Grid 
{
	private int n = ConfigureGame.HIGHER_BOUNDARY - ConfigureGame.LOWER_BOUNDARY +1;
	private BlockInterface[][] table;
	private int ships_on_grid;
	private static boolean flag = true;
	private int activate_sonar = 0;
	private int sonar_was_activated = 0;
	private int nr_sonar_pulse = 2;
	
	public static void gameOver()
	{
		flag = false;
	}
	
	public Grid()
	{
		this.table = new Block[n][n];
		this.ships_on_grid = 0;
	}
	
	public void positionShip(ShipInterface ship) throws IncorrectPositionException
	{
		
		this.ships_on_grid++;
		ship.positionShip(this.table);
	}
	
	private void showPartOfGrid(int cx, int cy)
	{
		this.nr_sonar_pulse--;
		if(this.nr_sonar_pulse == 0)
			this.activate_sonar = 0;
		int i,j;
		System.out.print("  ");
		for(i=1;i<=n;i++)
			System.out.print(" "+i+" ");
		System.out.println(" ");
		for(i=1;i<=n;i++)
		{
			System.out.print(i+" ");
			if(i == cx-2 || i == cx+2)
				for(j=1;j<=n;j++)
				{
					if(j == cy)
						if(table[i-1][j-1]!=null)
							System.out.print(" "+table[i-1][j-1].toString()+" ");
						else
							System.out.print(" - ");
					else
						System.out.print("   ");
				}
			else
				if(i == cx-1 || i == cx+1)
					for(j=1;j<=n;j++)
					{
						if(j == cy-1 || j == cy || j== cy+1)
							if(table[i-1][j-1]!=null)
								System.out.print(" "+table[i-1][j-1].toString()+" ");
							else
								System.out.print(" - ");
						else
							System.out.print("   ");
					}
				else
				{
					if(i == cx)
						for(j=1;j<=n;j++)
						{
							if(j == cy-2 || j == cy-1 || j == cy || j== cy+1 || j == cy+2)
								if(table[i-1][j-1]!=null)
									System.out.print(" "+table[i-1][j-1].toString()+" ");
								else
									System.out.print(" - ");
							else
								System.out.print("   ");
						}
				}
			System.out.println(" ");
		}
		System.out.println("LEGENDA: ");
		System.out.println("  * - parte lovita dintr-o nava\n  x - bloc intreg al unei nave");
	
	}
	
	public void fire()
	{
		int cx,cy,code;
		String aw = "NU"; //"Doriti sa folositi sonar pulse?"
		/*repeat se foloseste pentru a asigura ca jucatorul curent mai primeste o lovitura in cazurile:
		 		- lovitura anterioara a fost invalida
		 		- lovitura anterioara a fost un hit
		 		- raspunsul la intrebarea legata de sonar pulse este invalid
		 */
		int repeat = 0; 
		BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
		try
		{
			System.out.println("Se introduc coordonatele blocului atacat!");
			do
			{
				repeat = 0;
				if(this.activate_sonar == 1)
				{
					System.out.println("Mai aveti "+this.nr_sonar_pulse+" Sonar Pulse.Lanseaza?(DA / NU)");
					aw = buff.readLine();
				}
				System.out.println("Coordonata Orizontala (cx):= ");
				cx = Integer.parseInt(buff.readLine());
				System.out.println("Coordonata Verticala (cy):= ");
				cy = Integer.parseInt(buff.readLine());
				
				if (cx <= ConfigureGame.HIGHER_BOUNDARY && cx >= ConfigureGame.LOWER_BOUNDARY &&
					cy <= ConfigureGame.HIGHER_BOUNDARY && cy >= ConfigureGame.LOWER_BOUNDARY)
				{
					if(aw.equals("NU") || aw.equals("nu"))
					{
						System.out.println("Blocul ("+cx+","+cy+") a fost atacat!\nRezultat: ");
						if(table[cx-1][cy-1] == null)
						{
							System.out.println("MISSED!");
							table[cx-1][cy-1] = new Block(ConfigureGame.DEAD_BLOCK);
						}
						else
						{
							try {
								code = table[cx-1][cy-1].destroyBlock();
								if(code != ConfigureGame.DEAD_BLOCK)
								{
									//table[cx-1][cy-1] = new Block(ConfigureGame.DEAD_BLOCK);
									if(code == ConfigureGame.SHIP_SUNK_CODE)
									{
										if (this.activate_sonar == 0 && this.sonar_was_activated == 0 )
										{
											this.activate_sonar = 1;
											this.sonar_was_activated = 1;
											System.out.println("Sonar Pulse a fost activat!");
										}
										this.ships_on_grid--;
										if (this.ships_on_grid == 0)
											Grid.gameOver();
										else 
											repeat = 1; //mai ai dreptul la o lovitura
									}
									else
										repeat = 1; //a fost un hit => mai ai dreptul la o lovitura 
								}
								else
								{
									System.out.println("Acest bloc este mort!\nIntroduceti noile coordonate: ");
									repeat = 1;
								}
							} catch (MissException e) {
								System.out.println("MISSED!");
							}
						}
					}
					else
					{
						if(aw.equals("DA") || aw.equals("da"))
							//se afiseaza zona de pe grid conform pattern-ului cerut
							showPartOfGrid(cx,cy);
						else
						{
							System.out.println("Raspuns incorect");
							repeat = 1;
						}
					}
				}
				else
				{
					System.out.println("Coordonate incorecte!");
					repeat = 1;
				}
			}while(repeat == 1);
		}catch(IOException e){
			
		}
		
	}
	
	public static void main(String[] args) 
	{
		String s;
		int i,id;
		try
		{
			BufferedReader file = new BufferedReader(new InputStreamReader(new FileInputStream("conf_battleship.txt")));
			ConfigureGame.LOWER_BOUNDARY = Integer.parseInt(file.readLine());
			ConfigureGame.HIGHER_BOUNDARY = Integer.parseInt(file.readLine());
			ConfigureGame.NR_SHIPS = Integer.parseInt(file.readLine());
			ConfigureGame.SHIP_NR_BLOCKS = new int [ConfigureGame.NR_SHIPS];
			for(i=0;i<ConfigureGame.NR_SHIPS;i++)
			{
				s=file.readLine();
				StringTokenizer st = new StringTokenizer(s);
				id=Integer.parseInt(st.nextToken());
				ConfigureGame.SHIP_NR_BLOCKS[id] = Integer.parseInt(st.nextToken());
			}
			
		}catch(IOException e){
			
		}
		try
		{
			Grid grid = new Grid();
			Ship mines = new Minesweeper();
			grid.positionShip(mines);
			Ship battle = new Battleship();
			grid.positionShip(battle);
			Ship destr = new Destroyer();
			grid.positionShip(destr);
			while(flag)
				grid.fire();
			System.out.println(" SURRENDER !!!\n GAME OVER !!! ");
		}catch(IncorrectPositionException e)
		{
			System.out.println(e);
		}
   }
}		
